package com.heima.comment.service.impl;

import com.heima.api.wemedia.WemediaFeign;
import com.heima.comment.service.CommentReplyService;
import com.heima.model.comment.dtos.CommentRepayDto;
import com.heima.model.comment.dtos.CommentRepayLikeDto;
import com.heima.model.comment.dtos.CommentReplySaveDto;
import com.heima.model.comment.pojos.Comment;
import com.heima.model.comment.pojos.CommentReply;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.utils.common.AppThreadLocalUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
@Slf4j
@RequiredArgsConstructor
public class CommentReplyServiceImpl implements CommentReplyService {
    private final  MongoTemplate mongoTemplate;
    private final WemediaFeign wemediaFeign;
    @Override
    public ResponseResult addOrDeleteReply(CommentRepayLikeDto dto) {
        log.info("前段传入的数据为{}",dto);
        CommentReply commentReply=new CommentReply();
        if(StringUtils.isEmpty(dto.getOperation()) || (dto.getOperation() != 0&&dto.getOperation()!=1)){
            log.info("数据没法用");
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        Criteria criteria=new Criteria();
        criteria.andOperator(Criteria.where("id").is(dto.getCommentRepayId()),
                Criteria.where("content").ne(null));
        Query query=new Query();
        query.with(Sort.by(Sort.Direction.DESC, "createdTime"));
        List<CommentReply> comments = mongoTemplate.find(Query.query(criteria), CommentReply.class);
        if(!comments.isEmpty()){
            Long likes=0L;
            if(dto.getOperation()==1){
                likes=-1L;
            }else {
                likes=1L;
            }
            CommentReply comment = comments.get(0);
            if(comment.getLikes()==null){
                comment.setLikes((long)0);
                commentReply.setOperation(null);

            }else {
                commentReply.setOperation((short)0);
            }
            log.info("查出的数据为{}",comment);
            commentReply.setId(UUID.randomUUID().toString());
            commentReply.setCommentId(comment.getId());
            commentReply.setLikes(comment.getLikes()==0&& likes<0? 0:Long.parseLong(comment.getLikes().toString())+likes);
            commentReply.setContent(null);
            commentReply.setUserId(AppThreadLocalUtil.getUser().getId());
            commentReply.setUserName("");
            commentReply.setCreateTime(new Date());
            mongoTemplate.save(commentReply);
            return ResponseResult.okResult(commentReply.getLikes().toString());
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
    }

    @Override
    public ResponseResult addCommentReply(CommentReplySaveDto dto) {
        CommentReply commentReply=new CommentReply();
        Criteria criteria=new Criteria();
        criteria.andOperator(Criteria.where("id").is(dto.getCommentId()),
                Criteria.where("content").ne(null));
        if (!wemediaFeign.panduanSenistive(dto.getContent())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.MINGANCI_EEOR);
        }
        List<Comment> comments = mongoTemplate.find(Query.query(criteria), Comment.class);
        if(comments.isEmpty()){
           return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        Comment comment = comments.get(0);
        commentReply.setId(UUID.randomUUID().toString());
        commentReply.setCommentId(comment.getId());
        commentReply.setLikes(0L);
        commentReply.setContent(dto.getContent());
        commentReply.setUserId(AppThreadLocalUtil.getUser().getId());
        commentReply.setUserName("");
        commentReply.setCreateTime(new Date());
        mongoTemplate.save(commentReply);
        return ResponseResult.okResult(null);
    }

    @Override
    public ResponseResult queryCommentReply(CommentRepayDto dto) {
        dto.opop();
        if(StringUtils.isEmpty(dto.getCommentId())){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        Criteria criteria=new Criteria();
        criteria.andOperator(Criteria.where("commentId").is(dto.getCommentId()),
                Criteria.where("createTime").lt(dto.getMinDate()),
                Criteria.where("content").ne(null));
        Query query=new Query();
        query.limit(dto.getSize());
        query.with(Sort.by(Sort.Direction.DESC, "createdTime"));
        List<CommentReply> commentReplies = mongoTemplate.find(Query.query(criteria), CommentReply.class);
        //进行数据封装

        return ResponseResult.okResult(commentReplies);
    }
}
